package services

import (
	"fmt"
	common "jihe.common"
	"jihe.common/tool"
	"sort"
	"time"
)

type fabricItem struct {
	Discount int `json:"discount"`
}

func GetFabricDiscount(needId string) (int,error) {
	ret := make([]fabricItem, 0)
	rurl := fmt.Sprintf(`%s/fabrics/%s.json?%d`,common.Configer.Oss.Domain,needId,time.Now().Unix())
	_,_,errs := tool.Request.Get(rurl).Timeout(time.Second * 3).EndStruct(&ret)
	if len(errs) != 0{
		return 1,errs[0]
	}
	disCounts := make([]int,0)
	for _,v := range ret{
		disCounts = append(disCounts,v.Discount)
	}
	sort.Ints(disCounts)
	if len(disCounts) != 0{
		return disCounts[len(disCounts) - 1],nil
	}
	return 1,fmt.Errorf("缺少面料数据")
}